#!/bin/bash
#set -x
#set -e

source ./config/config
source ./script/html.sh
source ./script/lib.sh

result_dir=/tmp/check/
file_output="wincloud-check-$(hostname)-${CURRENT_TIME}.html"

myip=$(hostname -I |awk '{print $1}')
myhostname=$(hostname)
OS_INFO=$(cat /etc/os-release |grep PRETTY_NAME |awk -F'=' '{print $2}')
CPU_INFO=$(lscpu | grep -E "Architecture|op-mode|Model name")

os_check(){
				echo -e "\033[93m|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|"
				echo -e "|                        操作系统信息                     |"
				echo -e "|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\033[0m"

				get_cpu_info
				get_total_mem_info
				get_os_root_expire
				get_kernel_para
#				get_resource_limit
				get_selinux
				get_transparent_hugepage
				get_hugepage
				get_cpu_used
				get_mem_info
				get_disk_info
				get_net_io
				get_cron
				get_restart_time
				get_sys_log
				get_os_user_expire
}

os_check

## 获取各类型结果
FATAL_LIST=$(grep -r -l "FATAL" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
ERROR_LIST=$(grep -r -l "ERROR" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
NORMAL_LIST=$(grep -r -l "NORMAL" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
UNKOWN_LIST=$(grep -r -l "UNKOWN" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)
WARNING_LIST=$(grep -r -l "WARNING" $result_dir/*_result | awk -F"/" '{print $NF}' | sed s/_result//g)

##>>>>>>>>>>>>>>>>>创建html
create_html_css
create_a_head_ansible

# 左侧菜单栏
create_a_menu_head
# linux
create_one_level_menu_item "OS CHECKLIST"
for key in ${os_arr[*]}
do
	create_two_level_menu_item "$key" "${os_dict[$key]}"
done
create_one_level_menu_item_end

create_a_menu_end


create_a_content_box
create_a_headline
create_scroll_to_top

####第一个章节
create_html_head2 "巡检环境基本信息"
create_table_head "importance_info"
#for tr
create_tr2 "OS HOSTNAME" "$myhostname"
create_tr2 "OS VERSION" "$OS_INFO"
create_tr2 "CPU INFO" "$CPU_INFO"
create_tr2 "IP ADDRESS" "$myip"
create_tr2 "OS LAST RESTART" "$restart_time"
create_tr2 "OS USER EXPIRE TIME" "$os_dbuser_expire"

create_table_end

#### 第二个章节
create_html_head2 "巡检结果列表"
echo -e "<div id='db_server' style='transition: height 0.2s;'>">>$file_output
create_table_head "result_info"
create_tr3_h "Number" "Status" "Type" "Message" "Details"
create_db_server_info ${FATAL_LIST[*]} "FATAL"
create_db_server_info ${ERROR_LIST[*]} "ERROR"
create_db_server_info ${WARNING_LIST[*]} "WARNING"
create_db_server_info ${UNKOWN_LIST[*]} "UNKOWN"
create_db_server_info ${NORMAL_LIST[*]} "NORMAL"
create_table_end_no_br
echo -e "<div id='foldtable' style='width: 94%;' class='foldbtn'>展开</div>">>$file_output
echo -e "</div></br>">>$file_output

#### 第三个章节
create_html_head2 "操作系统巡检项详情"
for key in ${os_arr[*]}
do
	#create_a_hr
	create_html_head3 "$key" "${os_dict[$key]}" # 巡检项标题
	create_table_head_2 # 创建表头
	detail_file_name="$result_dir/$key""_detail"
	create_tr_detail "$detail_file_name" "$key"
	create_table_end
	create_suggestion "$key" "${os_suggestion[$key]}"
done

create_main_content_end
create_html_end

echo -e "\033[93m 巡检结果文件为:$file_output \033[0m"
